// 使用vuex管理路由规则
import { constantRoutes, asyncRoutes } from '@/router'
export default {
  state: {
    routes: constantRoutes
  },
  mutations: {
    // 设置路由
    setRoutes(state, filterRoutes) {
      // state.routes.push(...filterRoutes, { path: '*', redirect: '/404', hidden: true })
      state.routes = [...constantRoutes, ...filterRoutes, { path: '*', redirect: '/404', hidden: true }]
    }
  },
  actions: {
    getAsyncRoutes(context, menus) {
      const filterRoutes = asyncRoutes.filter(item => menus.includes(item.children[0].name))
      // 提交mutation
      context.commit('setRoutes', filterRoutes)
      return filterRoutes // 将过滤完的路由规则返回, 返回是为了permission中addRoutes中添加路由规则的
    }
  },
  namespaced: true
}
